package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.netflix.loadbalancer.IRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {
    //public static final String PAYMENT_URL= "http://localhost:8001";
    public static final String PAYMENT_URL= "http://cloud-provider-payment-consul";



    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/consul")
    public Object create(){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/consul", Object.class);

    }

    @GetMapping("/consumer/payment/getForEntity/{id}")
    public Object getForEntity(@PathVariable Integer id){
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);

        if (forEntity.getStatusCode().is2xxSuccessful()){
            return forEntity.getBody();
        }
        else {
            return new CommonResult<>(400,"操作失败");
        }

    }
}
